//
//  DebugRabbitMQViewController.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/14.
//

#import "DebugRabbitMQViewController.h"
#import "DebugRabbitMQManager.h"

@interface DebugRabbitMQViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating>
///
@property(nonatomic,strong) UITableView *tableView;

@property (nonatomic, strong) UISearchController *searchController;

@property (nonatomic, strong) NSArray *searchResult;
@end

@implementation DebugRabbitMQViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"RabbitMQ";
    [self.view addSubview:self.tableView];
}

#pragma mark -tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchController.active) {
        return self.searchResult.count;
    }
    return [DebugRabbitMQManager sharedInstance].records.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(UITableViewCell.class)];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NSStringFromClass(UITableViewCell.class)];
        cell.backgroundColor = UIColor.clearColor;
        cell.contentView.backgroundColor = UIColor.clearColor;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSDictionary *record = [NSDictionary dictionary];
    if (self.searchController.active) {
        record = self.searchResult[indexPath.row];
    } else {
        record = [DebugRabbitMQManager sharedInstance].records[indexPath.row];
    }
    cell.textLabel.text = record[debug_mq_key];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [format setTimeZone:[NSTimeZone systemTimeZone]];
    NSString *reTime = @"";
    if ([record[debug_mq_receiveDate] isKindOfClass:NSDate.class]) {
        reTime = [format stringFromDate:record[debug_mq_receiveDate]];
    }else
    {
        reTime = [NSString stringWithFormat:@"%@",record[debug_mq_receiveDate]];
    }
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@--%@",record[debug_mq_exchangeName],reTime];;
    
//    NSString *result = [[NSString alloc] initWithData:record[debug_mq_value] encoding:NSUTF8StringEncoding];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [NSDictionary dictionary];
    if (self.searchController.active) {
        dic = self.searchResult[indexPath.row];
    } else {
        dic = [DebugRabbitMQManager sharedInstance].records[indexPath.row];
    }
    NSString *key = dic[debug_mq_key];\
    NSString *value = [NSString stringWithFormat:@"%@",dic];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:key message:value preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    if(self.searchController.active)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    [self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark -----------getter/setter------------
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame) - (self.view.safeAreaInsets.top), self.view.bounds.size.width, self.view.bounds.size.height - CGRectGetMaxY(self.navigationController.navigationBar.frame) - (self.view.safeAreaInsets.top) - (self.view.safeAreaInsets.bottom)) style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 85;
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.backgroundColor = UIColor.clearColor;
        tableView.tableHeaderView = self.searchController.searchBar;
        _tableView = tableView;
    }
    return _tableView;
}
#pragma mark - UISearchResultsUpdating

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *inputStr = searchController.searchBar.text;
    NSMutableArray *arr = [NSMutableArray array];
    [[DebugRabbitMQManager sharedInstance].records enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull record, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([[record[debug_mq_key] lowercaseString] containsString:[inputStr lowercaseString]]) {
            //containsString（包含字符串）
            [arr addObject:record];
        }
    }];
    self.searchResult = arr;
    [self.tableView reloadData];
}

- (UISearchController *)searchController
{
    if (!_searchController) {
        _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        _searchController.searchResultsUpdater = self;
        _searchController.dimsBackgroundDuringPresentation = NO;
        _searchController.hidesNavigationBarDuringPresentation = YES;
    }
    return _searchController;
}
- (NSArray *)searchResult
{
    if(!_searchResult)
    {
        _searchResult = [NSArray array];
    }
    return _searchResult;
}
- (void)dealloc
{
    
}
@end
